package com.pys.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import org.apache.commons.lang.StringUtils;

import com.pys.common.beans.BaseBean;
import com.pys.enums.Statu;
import com.pys.pojo.PysKullanici;
import com.pys.pojo.PysRole;
import com.pys.service.KullaniciService;
import com.pys.service.RoleService;
import com.pys.utils.FacesUtils;
import com.pys.validators.EmailValidator;

@ManagedBean
@ViewScoped
public class KullaniciBean extends BaseBean implements Serializable {
	private final String DEFAULT_SIFRE = "PAROLA";
	private String ad;
	private String soyad;
	private String mail;
	private String sifre;
	private String sifreTekrar;
	private boolean durum = true;
	private PysKullanici selectedKullanici;
	private List<PysKullanici> list;
	private Long roleId;
	private List<SelectItem> roles;

	public KullaniciBean() {
		RoleService roleService = new RoleService();
		List<PysRole> list = roleService.getAllList(false);
		roles = new ArrayList<SelectItem>();
		for (PysRole pysRole : list) {
			roles.add(new SelectItem(pysRole.getId(), pysRole.getAd()));
		}
		refreshList();
	}

	/*
	 * ACTIONS
	 */
	public void refreshList() {
		KullaniciService kullaniciService = new KullaniciService();
		list = kullaniciService.getAllList(false);
	}

	public void saveAction() {

		if (StringUtils.isBlank(mail)) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
					"VALIDATION_MAIL");
			return;
		} else if (!EmailValidator.validate(mail)) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
					"VALIDATION_INVALID_MAIL");
			return;
		}
		if (roleId == null || roleId == 0) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
					"VALIDATION_ROLE");
			return;
		}

		KullaniciService kullaniciService = new KullaniciService();
		RoleService roleService = new RoleService();
		if (selectedKullanici == null) {
			PysKullanici pysKullanici = new PysKullanici();
			pysKullanici.setAd(ad);
			pysKullanici.setSoyad(soyad);
			pysKullanici.setMail(mail);
			pysKullanici.setSifre(DEFAULT_SIFRE);
			pysKullanici.setDurum(durum);
			pysKullanici.setOlusturmaTarih(new Date());
			pysKullanici.setPysKullanici(FacesUtils.getUserSession()
					.getCurrentUser());
			pysKullanici.setStatu(Statu.FIRST_LOGIN.toString());
			pysKullanici.setPysRole(roleService.getById(roleId));

			kullaniciService.save(pysKullanici);
		} else {
			selectedKullanici.setAd(ad);
			selectedKullanici.setSoyad(soyad);
			selectedKullanici.setMail(mail);
			// selectedKullanici.setSifre(sifre);
			selectedKullanici.setDurum(durum);
			selectedKullanici.setGuncellemeTarih(new Date());
			selectedKullanici.setPysKullanici(FacesUtils.getUserSession()
					.getCurrentUser());
			selectedKullanici.setPysRole(roleService.getById(roleId));
			kullaniciService.update(selectedKullanici);
		}
		refreshList();
		clear();
		FacesUtils.successMessage();
	}

	public void updateAction() {
		ad = selectedKullanici.getAd();
		soyad = selectedKullanici.getSoyad();
		mail = selectedKullanici.getMail();
		sifre = selectedKullanici.getSifre();
		roleId = selectedKullanici.getPysRole().getId();
		durum = selectedKullanici.isDurum();
	}

	public void deleteAction() {
		KullaniciService kullaniciService = new KullaniciService();
		selectedKullanici.setDeleted(true);
		kullaniciService.update(selectedKullanici);
		refreshList();
		clear();
		FacesUtils.successMessage();
	}

	public void clear() {
		selectedKullanici = null;
		ad = null;
		soyad = null;
		mail = null;
		sifre = null;
		sifreTekrar = null;
		roleId = null;
		durum = true;
	}

	/*
	 * GETTER_SETTER
	 */
	public boolean isDurum() {
		return durum;
	}

	public void setDurum(boolean durum) {
		this.durum = durum;
	}

	public String getAd() {
		return ad;
	}

	public void setAd(String ad) {
		this.ad = ad;
	}

	public String getSoyad() {
		return soyad;
	}

	public void setSoyad(String soyad) {
		this.soyad = soyad;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getSifre() {
		return sifre;
	}

	public void setSifre(String sifre) {
		this.sifre = sifre;
	}

	public String getSifreTekrar() {
		return sifreTekrar;
	}

	public void setSifreTekrar(String sifreTekrar) {
		this.sifreTekrar = sifreTekrar;
	}

	public PysKullanici getSelectedKullanici() {
		return selectedKullanici;
	}

	public void setSelectedKullanici(PysKullanici selectedKullanici) {
		this.selectedKullanici = selectedKullanici;
	}

	public List<PysKullanici> getList() {
		return list;
	}

	public void setList(List<PysKullanici> list) {
		this.list = list;
	}

	public List<SelectItem> getRoles() {
		return roles;
	}

	public void setRoles(List<SelectItem> roles) {
		this.roles = roles;
	}

	public Long getRoleId() {
		return roleId;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}

	public String getDefaultSifre() {
		return DEFAULT_SIFRE;
	}

}
